
import mixin from '@/mixin/mixin.js'
const app = getApp()
import {
  getDaysInMonth,
  getCurrentMonth,
  getCurrentYear,
  getCurrentDay
} from '@/utils/calendarUtils'
export default {
  mixins: [mixin],
  props: {
    // 日历类型 普通的就
    calendarType: {
      type: String,
      default: 'normal'
    },
    //绑定值
    value: {
      type: String,
      default: ''
    },
    //分隔符
    separator: {
      type: String,
      default: '-'
    },
    weekArr: {
      type: Array,
      default: ['日', '一', '二', '三', '四', '五', '六']
    },
    //当前月数据(有多少天)
    calendarData: {
      type: Array,
      default: getDaysInMonth()
    },
    //是否显示节假日
    showHoliday: {
      type: Boolean,
      default: false
    },
    //是否显示价格
    showPrice: {
      type: Boolean,
      default: false
    },
    //是否可选(当前时间)之前的日期
    canPickPrevDate: {
      type: Boolean,
      default: false
    },
    //头部背景
    headerBg: {
      type: String,
      default: '#fff'
    },
    //头部子项背景
    headerItemBg: {
      type: String,
      default: '#fff'
    },
    // 旅游日历 需要根据这个去查日历里面可选的日期
    travelCalendar: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      newCalendarData: this.calendarData,
      newValue: '', //选择的日期值
      currentYear: getCurrentYear(),
      currentMonth: getCurrentMonth(),
      currentDay: getCurrentDay()
    }
  },
  watch: {
    travelCalendar: {
		deep: true,
		handler(val) {
			if(val) {
				this.initCalendar();
			}
		}
    }
  },
  computed: {
    //窗口宽度
    windowWidth() {
      let { windowWidth } = app.globalData.systemInfo
      return windowWidth;
    },
  },
  mounted() {
    this.newValue = this.value;
    this.initCalendar('mounted');
  },
  methods: {
    initCalendar(type) {
      // 处理数据之前 先初始化数据
      this.newCalendarData = this.calendarData;
      //获取当前日在数组中的角标
      const idx = this.newCalendarData.lastIndexOf(this.currentDay)
      // this.newCalendarData[idx].
      //获取当前月第一天在数组中的角标
      const idxFirst = this.newCalendarData.lastIndexOf(1)
      //将小于当前日角标的都禁用
      this.newCalendarData = this.newCalendarData.reduce((arr = [], next, index) => {
        let price = null;
        (this.travelCalendar || []).map(i => {
          const dates = i.markDate.split('-')
          let year = dates[0], month = dates[1], day = dates[2];
          //低于10的先补齐 就不走时间戳转换了
          let currMonth = this.currentMonth < 10 ? `0${this.currentMonth}` : this.currentMonth;
          let currDay = next < 10 ? `0${next}` : next;
          // 如果是同一天  则塞入价格
          if (this.currentYear == year && currMonth == month && currDay == day) {
            price = i.startPrice;
          }
        })

        //当前年 当前月 
        if (this.currentYear == getCurrentYear() &&
          this.currentMonth == getCurrentMonth() &&
          index < idx) {
          /**
           * 当前日之前且 不可选当前时期之前的日期  禁用
           * 当前日之前且小于1号的日期  禁用
           * 当前日之前 但可选当前时期之前的日期 不禁用
           * 判断二同样适用
           */
          arr.push({ date: next, price, disable: index < idx && (!this.canPickPrevDate || index < idxFirst) })
        } else if (this.currentYear < getCurrentYear() || this.currentMonth < getCurrentMonth()) {
          //上以年或上一月 全部禁用
          arr.push({ date: next, price, disable: !this.canPickPrevDate || index < idxFirst })
        } else {
          //下一月
          if (this.currentYear >= getCurrentYear() && this.currentMonth > getCurrentMonth() ) {
            arr.push({ date: next, price, disable: index < idxFirst })
          } else {
            arr.push({ date: next, price })
          }
        }
        return arr
      }, [])
      if(type == 'mounted' && this.newValue){
        this.newCalendarData[idx].isCheck = true;
      }
    },
    //切换月份
    handleSwichMonth(e) {
      const { number } = e.currentTarget.dataset;
      this.currentMonth += number;
      //月份大于12 到下一年的1月
      if (this.currentMonth > 12) {
        this.currentMonth = 1;
        this.currentYear++;
      }
      //月份小于1 到上一年的12月
      if (this.currentMonth < 1) {
        this.currentMonth = 12;
        this.currentYear--;
      }
      this.newCalendarData = getDaysInMonth(this.getCalendarDataParams());
      this.initCalendar();
    },
    //获取日历的参数
    getCalendarDataParams() {
      const obj = {
        year: this.currentYear,
        month: this.currentMonth,
      }
      return obj;
    },
    //日历点击事件
    onTap(e) {
      const { item, idx} = e.currentTarget.dataset;
      if (item.disable) return
      this.newCalendarData = this.newCalendarData.map((i, index) => {
        if (index == idx) {
          i.isCheck = true;
          let currMonth = this.currentMonth < 10 ? `0${this.currentMonth}` : this.currentMonth;
          let day = i.date < 10 ? `0${i.date}` : i.date;
          this.newValue = `${this.currentYear}${this.separator}${currMonth}${this.separator}${day}`
          let pages = getCurrentPages(); // 当前页面
          let nowPage = pages[pages.length - 1]; // 前一个页面
          
          if(nowPage.route == 'pages/mall/index'){
            this.$emit('onChange', { date: this.newValue } )
          }
        } else {
          i.isCheck = false;
        }
        return { ...i }
      })
    },
  }
}